// use localStorage to store the authority info, which might be sent from server in actual project.

// 权限
export function getAuthority() {
  let auth = (localStorage.getItem('upmall-authority') || "").split(",");
  return auth;
}

export function setAuthority(authority) {
  return localStorage.setItem('upmall-authority', authority);
}

// 角色
export function getRole() {
  return localStorage.getItem('shuyun-role') || 'guest';
}

export function setRole(role) {
  return localStorage.setItem('shuyun-role', role);
}

// 商户账户
export function getAccount() {
  const account = sessionStorage.getItem('shuyun-account');
  if (account != 'undefined' && account != undefined && account != null && account != '') {
    return JSON.parse(account);
  }
  return null;
}

export function setAccount(account) {
  return sessionStorage.setItem('shuyun-account', JSON.stringify(account));
}

// 用户
export function getUser() {
  const user = sessionStorage.getItem('shuyun-user');
  if (user != 'undefined' && user != undefined && user != null && user != '') {
    return JSON.parse(user);
  }
  return null;
}

export function setUser(user) {
  return sessionStorage.setItem('shuyun-user', JSON.stringify(user));
}

// 权限判断
export function hasAuthority(needAuth) {
  if (needAuth) { return !(getAuthority().indexOf(needAuth) < 0); }
  return true;
}

// 角色判断
export function hasRole(needRole) {
  if (needRole) { return getRole() === needRole; }
  return true;
}

// 签约产品判断
export function hasProduct(needProductName) {
  let result = true;
  if (needProductName) {
    const account = getAccount();
    if (account && account.merchant && account.merchant.merchantProductDetail) {
      for (let p of account.merchant.merchantProductDetail) {
        if (p.product && p.product.name == needProductName) {
          result = true;
          break;
        }
      }
    }
  }
  return result;
}
